<?php 
header("content-type:text/html;charset=utf8");
$uid = $_POST['uid'];
$pdo = new PDO("mysql:host=127.0.0.1;dbname=07c","root","root");
$sql = "select * from store where store_id = '$uid'";
// print_r($sql);
$data = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
// print_r($res);die;
$stock = $data[0]['store_stock'];
// print_r($stock);die;
$redis = new redis();
$redis->connect("127.0.0.1","6379");
// for ($i=1; $i <=$stock; $i++) { 
// 	$redis->lpush("goods_".$uid,$i);
// }die;
if($stock>0)
{
	$sql = "update store set store_stock = $stock-1 where store_id =$uid";
	// print_r($sql);
	$res = $pdo->exec($sql);
	// print_r($res);
	if($res)
	{
		$order_id = "SM".rand(11111111,99999999).time();
		$goods_id = $data[0]['store_id'];
		$addtime = date("Y-m-d m:s:i");
		$sql = "insert into goods values('','$order_id','$goods_id','$addtime')";
		$res = $pdo->exec($sql);
		if($res)
		{
			$data = array("code"=>1,"msg"=>"抢购成功，生成订单");
		}
		else
		{
			$data = array("code"=>2,"msg"=>"抢购成功，未生成订单");
		}
	}
	else
	{
		$data = array("code"=>3,"msg"=>"抢购未成功");
	}
}
else
{
	$data = array("code"=>0,"msg"=>"抱歉，抢购完毕");
}
echo json_encode($data);


 ?>